package com.xc.disk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.disk.bean.PagingBean;
import com.xc.disk.bean.SafetyChainBean;
import com.xc.disk.dto.PagingDto;
import com.xc.disk.entity.SafetyChainEntity;
import com.xc.disk.enums.FailCode;
import com.xc.disk.info.TokenInfo;
import com.xc.disk.mapper.SafetyChainMapper;
import com.xc.disk.service.SafetyChainService;
import com.xc.disk.utils.ServiceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 安全链接实现类</p>
 * <P>创建人:小程 </p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/12/19</p>
 *
 * @version v1.0
 */
@Service
public class SafetyChainServiceImpl extends ServiceImpl<SafetyChainMapper, SafetyChainEntity> implements SafetyChainService {


    @Override
    public PagingDto<SafetyChainEntity> getSafetyChainPaging(TokenInfo token, PagingBean<SafetyChainEntity> pagingBean) {
        QueryWrapper<SafetyChainEntity> queryWrapper = ServiceUtils.queryData(pagingBean, pagingBean.getData(), token);
        IPage<SafetyChainEntity> iPage = this.page(new Page<>(pagingBean.getCurrent(), pagingBean.getSize()), queryWrapper);
        return new PagingDto<>(iPage.getTotal(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public SafetyChainEntity createSafetyChain(String userId, SafetyChainBean safetyChainBean) {
        SafetyChainEntity safetyChainEntity = new SafetyChainEntity();
        safetyChainEntity.setUserId(userId);
        safetyChainEntity.setUrl(safetyChainBean.getUrl());
        //判断用户的url是否重复
        List<SafetyChainEntity> chainEntities = this.list(new QueryWrapper<>(safetyChainEntity));
        if (chainEntities != null && chainEntities.size() != 0) {
            throw FailCode.URL_REPEAT.getOperateException();
        }
        safetyChainEntity.setName(safetyChainBean.getName());
        if (!this.save(safetyChainEntity)) {
            throw FailCode.SAFETY_CHAIN_CREATE_FAIL.getServerException();
        }
        return safetyChainEntity;
    }

    @Override
    public SafetyChainEntity updateSafetyChain(String userId, String id, SafetyChainBean safetyChainBean) {
        SafetyChainEntity safetyChainEntity = new SafetyChainEntity();
        safetyChainEntity.setUrl(safetyChainBean.getUrl());
        safetyChainEntity.setUserId(userId);
        List<SafetyChainEntity> chainEntities = this.list(new QueryWrapper<>(safetyChainEntity));
        //判断用户的url是否重复
        if (chainEntities != null && chainEntities.size() != 0) {
            throw FailCode.URL_REPEAT.getOperateException();
        }
        //判断安全链接是否存在
        safetyChainEntity = this.getById(id);
        if (safetyChainEntity == null) {
            throw FailCode.SAFETY_CHAIN_NOT_EXIST.getOperateException();
        }
        //判断安全链接是否是当前用户的
        if (!safetyChainEntity.getUserId().equals(userId)) {
            throw FailCode.NOT_THE_CREATOR.getOperateException();
        }
        //修改
        safetyChainEntity.setName(safetyChainBean.getName());
        safetyChainEntity.setUrl(safetyChainBean.getUrl());
        if (!this.updateById(safetyChainEntity)) {
            throw FailCode.SAFETY_CHAIN_UPDATE_FAIL.getServerException();
        }
        return safetyChainEntity;
    }

    @Override
    @Transactional
    public void deleteSafetyChain(String userId, List<String> ids) {
        for (String id : ids) {
            SafetyChainEntity safetyChainEntity = this.getById(id);
            if (safetyChainEntity == null) {
                throw FailCode.SAFETY_CHAIN_NOT_EXIST.getOperateException();
            }
            if (!safetyChainEntity.getUserId().equals(userId)) {
                throw FailCode.NOT_THE_CREATOR.getOperateException();
            }
            if (!this.removeById(id)) {
                throw FailCode.SAFETY_CHAIN_DELETE_FAIL.getServerException();
            }
        }
    }
}
